using System.Numerics;
using TTEngine.Graphics;
using TTEngine.Resource;
using TTEngine.TTMath;
using voidbox;

namespace TTEngine.Framework.UI
{
    public class UIImage : UIBaseElement
    {
        private PackedTextureSprite _sprite = null;
        private SingleTextureSprite _spriteSingle = null;

        private bool _keepAspect;

        public UIImage()
        {
            //_sprite = new PackedTextureSprite(Runtime.DefaultResource.PackedTextureGroup, Runtime.DefaultResource.BlockWhite);
            //QUIResource.GetWhiteSprite();
            LocalRect.SetByRect(new Rectangle(0, 0, 100, 100));
        }

        public void SetBySprite(PackedTextureSprite sprite)
        {
            _sprite = sprite;
        }
        //显示随意的未打包的Texture的能力也是需要的
        public void SetByTexture(Texture2D tex)
        {

            _spriteSingle = new SingleTextureSprite(tex, fAABB.One);
        }

        public void SetBySpriteElement(PackedTextureGroup pack, PackedTextureElement spriteElement)
        {
            _sprite = new PackedTextureSprite(pack, spriteElement);
        }


        public override void OnRender(UICanvas canvas)
        {
            if (_sprite != null)
            {
                var rect = GetWorldRect();
                if (_keepAspect)
                {
                    float oldHeight = rect.Height;
                    float oldWidth = rect.Width;
                    float asp = _sprite.Info.PixelHeight / (float)_sprite.Info.PixelWidth;
                    float aspRect = oldHeight / oldWidth;

                    if (aspRect > asp) // 越大越瘦高
                    {
                        var Height = oldWidth * asp;
                        rect.y1 += (oldHeight - rect.Height) / 2;
                        rect.y2 = rect.y1 + Height;
                    }
                    else if (aspRect < asp)
                    {
                        var Width = oldHeight / asp;
                        rect.x1 += (oldWidth - rect.Width) / 2;
                        rect.x2 = rect.x1 + Width;
                    }
                }
                canvas.Batcher.GetPackedTextureBatcher().DrawSprite(_sprite, rect, 0, _colorFinal);
                //_sprite.RenderRect(canvas._batcherUI, rect, _colorFinal, -1);
            }
            else if(_spriteSingle!=null)
            {
                var rect = GetWorldRect();
                if (_keepAspect)
                {
                    float oldHeight = rect.Height;
                    float oldWidth = rect.Width;
                    float asp = _spriteSingle.PixelHeight / (float)_spriteSingle.PixelWidth;
                    float aspRect = oldHeight / oldWidth;

                    if (aspRect > asp) // 越大越瘦高
                    {
                        var Height = oldWidth * asp;
                        rect.y1 += (oldHeight - rect.Height) / 2;
                        rect.y2 = rect.y1 + Height;
                    }
                    else if (aspRect < asp)
                    {
                        var Width = oldHeight / asp;
                        rect.x1 += (oldWidth - rect.Width) / 2;
                        rect.x2 = rect.x1 + Width;
                    }
                }
                canvas.Batcher.GetSingleTextureBatcher().Draw(_spriteSingle, rect, 0, _colorFinal);
                //_sprite.RenderRect(canvas._batcherUI, rect, _colorFinal, -1);
            }
            base.OnRender(canvas);
        }
    }
}